package com.msmall.community.score.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.community.common.model.ScoreRule;
import com.msmall.community.common.service.ScoreRuleManager;
import com.msmall.community.score.dao.ScoreDao;
import com.msmall.community.score.model.Score;
import com.msmall.community.score.service.ScoreManager;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.service.UserManager;

public class ScoreManagerImpl extends BaseManagerImpl<Score, Long> implements ScoreManager{

	private static final Log logger = LogFactory.getLog(ScoreManagerImpl.class);
	
	private ScoreDao scoreDao;

	private UserManager userManager;
	
	private ScoreRuleManager scoreRuleManager;
	
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	public void setScoreRuleManager(ScoreRuleManager scoreRuleManager) {
		this.scoreRuleManager = scoreRuleManager;
	}
	public void setScoreDao(ScoreDao scoreDao) {
		this.scoreDao = scoreDao;
	}
	/** 新增加的积分存入银行. 在线用户，用户退出时必须存入数据库
	 * +加分 -减分
	 */
	public void addScore(Long userid, int score) {
		addScore(userid, score, false);
	}

	//不在线用户。 true立刻刷新数据库  +加分 -减分
	public void addScore(Long userid, int score, boolean flush){
		UserInfo userInfo = userManager.getByUserid(userid);
		Score scorex = userInfo.getScore();
		scorex.setScore(userInfo.getScore().getScore() + score);
		userManager.saveUserInfo(userInfo);
	}

	/**
	 * 给用户添加积分
	 * @param userid
	 * @param key 积分规则的英文名称
	 */
	public void addScore(Long userid, String key, boolean flush){
		try {
			ScoreRule sr = scoreRuleManager.getScoreRule(key);
			if(sr == null){
				logger.error("Score Rule with key : " + key + " not exist");
			}else if(ConstantDB.UNAVALIABLE.compareTo(sr.getEnabled()) == 0){
				logger.error("Score Rule with key : " + key + " not enabled");
			}else{
				addScore(userid, sr.getScore(), flush);
			}
		} catch (Exception e) {
			logger.error(e);
		}
		
	}
	public void addThread(Long userid, boolean flush) {
		addScore(userid, ScoreRule.BBS_THREAD_ADD_KEY, flush);
	}

	public void addTopic(Long userid, boolean flush) {
		addScore(userid, ScoreRule.BBS_TOPIC_ADD_KEY, flush);
	}

	public void addTopicElite(Long userid, boolean flush) {
		addScore(userid, ScoreRule.BBS_TOPIC_ELITE_KEY, flush);
	}

	public void reduceThread(Long userid, boolean flush) {
		addScore(userid, ScoreRule.BBS_THREAD_DELETE_KEY, flush);
	}

	public void reduceTopic(Long userid, boolean flush) {
		addScore(userid, ScoreRule.BBS_TOPIC_DELETE_KEY, flush);
	}

	public void addCopy(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_COPY_ADD_KEY, flush);
	}

	public void addCopyCopy(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_COPY_COPY_KEY, flush);
	}

	public void addCopyDiary(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_DIARY_COPY_KEY, flush);
	}

	public void addDiary(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_DIARY_ADD_KEY, flush);
	}

	public void addEliteCopy(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_COPY_ELITE_KEY, flush);
	}

	public void addEliteDiary(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_DIARY_ELITE_KEY, flush);
	}

	public void addElitePhoto(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_PHOTO_ELITE_KEY, flush);
	}

	public void addFavoriteDiary(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_DIARY_FAVORITE_KEY, flush);
	}

	public void addFavoritePhoto(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_PHOTO_FAVORITE_KEY, flush);
	}

	public void addNormalLogin(Long userid, boolean flush){
		addScore(userid, ScoreRule.BASE_NORMAL_LOGIN_KEY, flush);
	}

	public void addPhoto(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_PHOTO_ADD_KEY, flush);
	}

	public void addRegister(Long userid, boolean flush) {
		addScore(userid, ScoreRule.BASE_REGISTER_KEY, flush);
	}

	public void addRosterRegister(Long userid, boolean flush) {
		addScore(userid, ScoreRule.BASE_ROSTER_KEY, flush);
	}

	public void addVipLogin(Long userid, boolean flush) {
		addScore(userid, ScoreRule.BASE_VIP_LOGIN_KEY, flush);
	}

	public void reduceCopy(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_COPY_DELETE_KEY, flush);
	}

	public void reduceDiary(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_DIARY_DELETE_KEY, flush);
	}

	public void reducePhoto(Long userid, boolean flush) {
		addScore(userid, ScoreRule.SPACE_PHOTO_DELETE_KEY, flush);
	}
}
